<template>
  <el-dialog
    v-model="dialogVisible"
    title="监控对接"
    width="830"
    class="qc-custom-dialog"
  >
    <el-scrollbar>
      <div class="code-info">
        <el-form
          ref="updateRef"
          :model="ruleForm"
          :rules="rules"
          label-width="100px"
          class="edit-form-wrap"
        >
          <el-form-item label="监控对接" prop="equipmentModelType">
            <el-radio-group
              v-model="ruleForm.equipmentModelType"
              v-for="(item, index) in options.equipmentModelType"
              :key="index"
            >
              <el-radio :label="item.value">{{ item.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <template v-if="ruleForm.equipmentModelType === '0'">
            <div v-for="(item, index) in ruleForm.data" :key="index" class="monitor-item-wrap">
              <el-form-item :key="index" :prop="`data.[${index}]deviceSerial`" :rules="{ required: true, message: '请输入设备序列号', trigger: 'blur' }" label="设备序列号">
                <el-input v-model="item.deviceSerial" placeholder="请输入设备序列号" maxlength="10" style="width:200px;" />
              </el-form-item>
              <el-form-item :key="index" :prop="`data.[${index}]devicePwd`" :rules="{ required: true, message: '请输入设备密码', trigger: 'blur' }" label="设备密码">
                <el-input v-model="item.devicePwd" placeholder="请输入设备密码" maxlength="20" style="width:200px;" />
              </el-form-item>
              <div style="margin-left: 20px;">
                <el-button v-if="index === ruleForm.data.length - 1 && index < ruleForm.maxLen - 1" type="primary" size="small"
                  @click="clickAdd(updateRef)">增加</el-button>
                <el-button v-if="ruleForm.data.length > 1 && index !== 0" type="danger" size="small"
                  @click="clickRemove(index)">删除</el-button>
              </div>
            </div>
          </template>
        </el-form>
      </div>
    </el-scrollbar>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="hide">取消</el-button>
        <el-button type="primary" v-btnAntiShake="onSubmit"> 保存 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { FormInstance, FormRules } from 'element-plus';
import { queryMonitorConfig, setMonitorConfig } from '@/service/apis/monitor'

let dialogVisible = ref(false);
const updateRef = ref<FormInstance>();

const options = reactive({
  equipmentModelType: [{
    label: '是',
    value: '0',
  }, {
    label: '否',
    value: '1',
  }] as any,
});

const ruleForm = reactive({
  stationId: '',
  equipmentModelType: '1',
  data: [
    {
      deviceSerial: '',
      devicePwd: ''
    }
  ],
  maxLen: 10,
});

const rules = reactive<FormRules>({
  equipmentModelType: [
    {
      required: true,
      message: '请选择监控对接',
      trigger: ['blur', 'change'],
    },
  ],
});

// 查询监控设置
const getMonitorConfig = () => {
  queryMonitorConfig(ruleForm.stationId).then((res:any) => {
    // console.log(res)
    ruleForm.equipmentModelType = res?.isAccess === 1 ? '0' : '1'
    ruleForm.data = res?.deviceConfigs || [{
      deviceSerial: '',
      devicePwd: ''
    }]
  })
}

// 显示弹框
const show = (row: any) => {
  ruleForm.stationId = row.id;
  ruleForm.equipmentModelType = '1';
  ruleForm.data = [{
    deviceSerial: '',
    devicePwd: ''
  }]
  getMonitorConfig()
  updateRef.value?.clearValidate('deviceSerial');
  updateRef.value?.clearValidate('devicePwd');
  dialogVisible.value = true;
};
// 关闭弹框
const hide = () => {
  dialogVisible.value = false;
};

const clickAdd = (formEl: FormInstance | undefined) => {
  if (ruleForm.data.length === ruleForm.maxLen) {
    return
  }
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      const data = {
        deviceSerial: '',
        devicePwd: ''
      }
      ruleForm.data.push(data)
    }
  })
}
const clickRemove = (index:number) => {
  ruleForm.data.splice(index, 1)
}

const onSubmit = async () => {
  if (!updateRef.value) return;
  await updateRef.value.validate((valid: any, fields: any) => {
    if (valid) {
      let form:any = {
        stationId: ruleForm.stationId,
      };
      if (ruleForm.equipmentModelType === '0') {
        form.deviceConfigs = ruleForm.data
      }
      console.log('form---', form);
      setMonitorConfig(form).then((res: any) => {
        hide();
      });
    } else {
      console.log('error submit!', fields);
    }
  });
};

defineExpose({
  show,
});
</script>

<style lang="scss" scoped>
.code-info {
  max-height: 50vh;
}
.monitor-item-wrap {
  display: flex;
  align-items: baseline;
}
</style>